﻿using brand.admin.cyt369.com.services.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Text;

namespace brand.admin.cyt369.com.services.ORM
{
    public partial class BrandContext:DbContext
    {
        public BrandContext()
        {
        }

        public BrandContext(DbContextOptions<BrandContext> options) : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (Admin.CYT369.Com.Utility.SystemSettingService.Instance.brand_admin_cyt369_com_connection_string.DBType == "SQLSERVER2008")
            {
                optionsBuilder.UseSqlServer(Admin.CYT369.Com.Utility.SystemSettingService.Instance.news_admin_cyt369_com_connection_string.ConnectionString).ReplaceService<Microsoft.EntityFrameworkCore.Query.IQueryTranslationPostprocessorFactory, Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServer2008QueryTranslationPostprocessorFactory>();
            }
            else
            {
                optionsBuilder.UseSqlServer(Admin.CYT369.Com.Utility.SystemSettingService.Instance.brand_admin_cyt369_com_connection_string.ConnectionString);
            }
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<brand.admin.cyt369.com.services.Models.Brand>(entity =>
            {
                entity.HasKey(c => c.Id);
            });

            builder.Entity<brand.admin.cyt369.com.services.Models.BrandExtension>(entity =>
            {
                entity.HasKey(c => c.Id);
                // 1对1
                entity.HasOne(c => c.Brand).WithOne(c => c.BrandExtension).HasForeignKey<brand.admin.cyt369.com.services.Models.BrandExtension>(c => c.BrandID);
            });

            builder.Entity<brand.admin.cyt369.com.services.Models.BrandImage>(entity =>
            {
                entity.HasKey(c => c.Id);
                // 1对多 品牌-品牌图片
                entity.HasOne(c => c.Brand).WithMany(c => c.BrandImages).HasForeignKey(c => c.BrandID);
            });

            builder.Entity<brand.admin.cyt369.com.services.Models.Carousel>(entity =>
            {
                entity.HasKey(c => c.Id);
                // 1对多 品牌-品牌轮播图
                entity.HasOne(c => c.Brand).WithMany(c => c.Carousels).HasForeignKey(c => c.BrandID);
            });

            builder.Entity<brand.admin.cyt369.com.services.Models.Category>(entity =>
            {
                entity.HasKey(c => c.Id);
                //一对多 品牌-类别
                entity.HasMany(c => c.Brands).WithOne(c => c.Category).HasForeignKey(c => c.CategoryID);
            });

            builder.Entity<brand.admin.cyt369.com.services.Models.HotBrand>(entity =>
            {
                entity.HasKey(c => c.Id);
                entity.HasOne(c => c.Brand).WithOne(c => c.HotBrand).HasForeignKey<HotBrand>(c=>c.BrandID);
            });

            builder.Entity<brand.admin.cyt369.com.services.Models.Inquiry>(entity =>
            {
                entity.HasKey(c => c.Id);
                entity.HasOne(c => c.Brand).WithMany(c => c.Inquiries).HasForeignKey(c => c.BrandID);
            });
        }
    }
}
